<?php

/**
 * 携程 定时 跑房态 房价
 * @author lzp300
 * @copyright 2015-03-26
 */

class qmgTwo extends actionAbstract{
    
    /**
     * 构造函数
     * @var array
     */
    function __construct() {
        parent::__construct();
	    set_time_limit(0);
    }
	
	//酒店 数组 字符串
	function nowHotel(){
		$this->loadModel('xc','uphotel');
		$in="";
		$re=$this->xc->uphotelModel->fetchAll("select hid from xc_uphotel where status=1 limit 150,150");
		foreach($re as $k=>$v){
			$in.="'".$v['hid']."',";
		}
		$in=trim($in,",");
		
		return $in;
	}
	
	//推送房态--房价
	function pytSP(){
		$this->loadApi('orderapi');
		$orderapi = new orderapi();
		$this->loadModel('qm','room');
		
		$in=$this->nowHotel();
		$sql="select a.*,b.tqydts from qm_room as a left join qm_hotel as b on b.hid=a.hid where a.hid in (".$in.")";
		$sql_to="select count(a.id) as thcount from qm_room as a left join qm_hotel as b on b.hid=a.hid where a.hid in (".$in.")";
		
		$reto=$this->qm->roomModel->fetchRow($sql_to);
		$count=$reto['thcount'];
		
		//获取房态价格
		if($count<=50){
			$re=$this->qm->roomModel->fetchAll($sql);
			foreach($re as $k=>$v){
				$sta=date('Y-m-d',strtotime("+".$v['tqydts']." day"));
				$end=date('Y-m-d',strtotime($sta)+20*24*3600);
				$resout=$orderapi->roombatch($v['hid'],$v['rid'],$sta,$end);
				if(!empty($resout['list']['room']['roomDatelist'])){
					$this->SP($resout['list']['room']['roomDatelist'],$v['hid'],$v['rid']);
				}
			}
		}else{
			$page=ceil($count/50);
			for($i=0;$i<$page;$i++){
				$re=$this->qm->roomModel->fetchAll($sql." limit ".($i*50).",50");
				foreach($re as $k=>$v){
					$sta=date('Y-m-d',strtotime("+".$v['tqydts']." day"));
					$end=date('Y-m-d',strtotime($sta)+20*24*3600);
					$resout=$orderapi->roombatch($v['hid'],$v['rid'],$sta,$end);
					if(!empty($resout['list']['room']['roomDatelist'])){
						$this->SP($resout['list']['room']['roomDatelist'],$v['hid'],$v['rid']);
					}
				}
			}
			
		}
	}
	
	//房态价格 存库  发送OSII
	function SP($resource,$hid,$rid){
		$this->loadModel('qm','roombatch');
		
		$statusArr=$priceArr=array();
		if(count($resource)==1){
			$data['hid'] = $hid;
			$data['rid'] = $rid;
			$data['date'] = !empty($resource['@attributes']['date'])?$resource['@attributes']['date']:'';
			$data['price'] = !empty($resource['@attributes']['price'])?$resource['@attributes']['price']:0;
			$data['roomStatus'] = !empty($resource['@attributes']['roomStatus'])?$resource['@attributes']['roomStatus']:0;
			$data['maxNum'] = !empty($resource['@attributes']['maxNum'])?$resource['@attributes']['maxNum']:0;
			$data['paymentMode'] = !empty($resource['@attributes']['paymentMode'])?$resource['@attributes']['paymentMode']:0;
			$data['redFree'] = !empty($resource['@attributes']['redFree'])?$resource['@attributes']['redFree']:0;
			$data['commentFree'] = !empty($resource['@attributes']['commentFree'])?$resource['@attributes']['commentFree']:0;
			$data['breakfast'] = !empty($resource['@attributes']['breakfast'])?$resource['@attributes']['breakfast']:'不含早';
			$data['created'] = date('Y-m-d H:i:s',time());
			
			$rq=$this->qm->roombatchModel->fetchRow("select id from qm_roombatch where hid=".$hid." and rid=".$rid." and date='".@$resource['@attributes']['date']."'");
			if($rq){$qs=$this->qm->roombatchModel->update($data,"id=".$rq['id']);}
			else{$qs=$this->qm->roombatchModel->insert($data);}
			
			$statusArr[0]['maxNum']=!empty($resource['@attributes']['maxNum'])?$resource['@attributes']['maxNum']:0;
			$statusArr[0]['date']=!empty($resource['@attributes']['date'])?$resource['@attributes']['date']:'';
			$statusArr[0]['roomStatus']=!empty($resource['@attributes']['roomStatus'])?$resource['@attributes']['roomStatus']:0;
			
			$priceArr[0]['date']=!empty($resource['@attributes']['date'])?$resource['@attributes']['date']:'';
			$priceArr[0]['price']=!empty($resource['@attributes']['price'])?$resource['@attributes']['price']:0;
			$breakfast=0;
			if(!empty($resource['@attributes']['breakfast'])&&($resource['@attributes']['breakfast']=="1份"||$resource['@attributes']['breakfast']=="含早餐")){
				$breakfast=1;
			}elseif(!empty($resource['@attributes']['breakfast'])&&$resource['@attributes']['breakfast']=="2份"){
				$breakfast=2;
			}
			$priceArr[0]['breakfast']=$breakfast;
		}else{
			foreach($resource as $ka=>$va){
				$data['hid'] = $hid;
				$data['rid'] = $rid;
				$data['date'] = !empty($va['@attributes']['date'])?$va['@attributes']['date']:'';
				$data['price'] = !empty($va['@attributes']['price'])?$va['@attributes']['price']:0;
				$data['roomStatus'] = !empty($va['@attributes']['roomStatus'])?$va['@attributes']['roomStatus']:0;
				$data['maxNum'] = !empty($va['@attributes']['maxNum'])?$va['@attributes']['maxNum']:0;
				$data['paymentMode'] = !empty($va['@attributes']['paymentMode'])?$va['@attributes']['paymentMode']:0;
				$data['redFree'] = !empty($va['@attributes']['redFree'])?$va['@attributes']['redFree']:0;
				$data['commentFree'] = !empty($va['@attributes']['commentFree'])?$va['@attributes']['commentFree']:0;
				$data['breakfast'] = !empty($va['@attributes']['breakfast'])?$va['@attributes']['breakfast']:'不含早';
				$data['created'] = date('Y-m-d H:i:s',time());
				
				$rq=$this->qm->roombatchModel->fetchRow("select id from qm_roombatch where hid=".$hid." and rid=".$rid." and date='".@$va['@attributes']['date']."'");
				if($rq){$qs=$this->qm->roombatchModel->update($data,"id=".$rq['id']);}
				else{$qs=$this->qm->roombatchModel->insert($data);}
				
				$statusArr[$ka]['maxNum']=!empty($va['@attributes']['maxNum'])?$va['@attributes']['maxNum']:0;
				$statusArr[$ka]['date']=!empty($va['@attributes']['date'])?$va['@attributes']['date']:'';
				$statusArr[$ka]['roomStatus']=!empty($va['@attributes']['roomStatus'])?$va['@attributes']['roomStatus']:0;
				
				$priceArr[$ka]['date']=!empty($va['@attributes']['date'])?$va['@attributes']['date']:'';
				$priceArr[$ka]['price']=!empty($va['@attributes']['price'])?$va['@attributes']['price']:0;
				$breakfast=0;
				if(!empty($va['@attributes']['breakfast'])&&($va['@attributes']['breakfast']=="1份"||$va['@attributes']['breakfast']=="含早餐")){
					$breakfast=1;
				}elseif(!empty($va['@attributes']['breakfast'])&&$va['@attributes']['breakfast']=="2份"){
					$breakfast=2;
				}
				$priceArr[$ka]['breakfast']=$breakfast;
			}
		}
		
		$this->HotelStatus($statusArr,$hid,$rid,2);
		$this->HotelPrice($priceArr,$hid,$rid);
	}
	
	//房态推送  @type 1关闭 否则 原状态
	function HotelStatus($array=array(),$hid=0,$rid=0,$type=1){
		$this->loadApi('qmgTwoLog');
		$qmgTwoLog = new qmgTwoLog();
		
		$body['startDate']=$body['endDate']="";
		$body['hotelCode']=$hid;
		$body['roomID']=$rid;
		$body['sourceId']=2;
		$body['editer']='qmg';$body['roomInfoList']=array();
		
		if($array){
			$body['startDate']=$array[0]['date'];
			$body['endDate']=$array[(count($array)-1)]['date'];
			
			$timeDa=$timeDb=$arrSort=array();$count=(strtotime($body['endDate'])-strtotime($body['startDate']))/24/3600;
			for($j=0;$j<$count;$j++){
				$timeDa[]=date('Y-m-d',(strtotime($body['startDate'])+$j*24*3600));
			}
			foreach($array as $k=>$v){$timeDb[]=$v['date'];}
			foreach($timeDa as $k=>$v){
				if(!in_array($v,$timeDb)){
					$count++;
					$array[$count]['maxNum']=0;
					$array[$count]['date']=$v;
					$array[$count]['roomStatus']=0;
				}
			}
			foreach($array AS $uniqid => $row){$arrSort[] = $row['date'];}
			array_multisort($arrSort, SORT_ASC, $array);
			
			$i=0;
			foreach($array as $k=>$v){
				$body['roomInfoList'][$i]['fl']=$v['maxNum'];
				$body['roomInfoList'][$i]['setToDate']=$v['date'];
				$body['roomInfoList'][$i]['changeDefault']='T';
				$body['roomInfoList'][$i]['holdDeadline']='9999';
				$body['roomInfoList'][$i]['note']='';
				$body['roomInfoList'][$i]['reserveTime']='0';
				$body['roomInfoList'][$i]['roomstatus']=$type!=1&&$v['roomStatus']==1&&$v['maxNum']>0?'G':'N';
				$body['roomInfoList'][$i]['roomsInv']='0';
				$body['roomInfoList'][$i]['guarantee']='';
				$body['roomInfoList'][$i]['guaranteeLCT']=4;
				$body['roomInfoList'][$i]['prepayLCT']=23988;
				$body['roomInfoList'][$i]['allNeedGuarantee']='F';
				$body['roomInfoList'][$i]['lateReserveTime']=0;
				$body['roomInfoList'][$i]['deductType']='C';
				$body['roomInfoList'][$i]['blanceType']='PP';
				$body['roomInfoList'][$i]['restorable']='F';
				$body['roomInfoList'][$i]['checkType']='C';
				$body['roomInfoList'][$i]['userLimited']='9999';
				$i++;
			}
			$qmgTwoLog->roomstatus($body);
		}
	}
	
	//价格推送
	function HotelPrice($array=array(),$hid=0,$rid=0){
		$this->loadApi('qmgTwoLog');
		$qmgTwoLog = new qmgTwoLog();
		
		if($array){
			$startDate=$array[0]['date'];
			$endDate=$array[(count($array)-1)]['date'];
			
			$timeDa=$timeDb=$arrSort=array();$count=(strtotime($endDate)-strtotime($startDate))/24/3600;
			for($j=0;$j<$count;$j++){
				$timeDa[]=date('Y-m-d',(strtotime($startDate)+$j*24*3600));
			}
			foreach($array as $k=>$v){$timeDb[]=$v['date'];}
			foreach($timeDa as $k=>$v){
				if(!in_array($v,$timeDb)){
					$count++;
					$array[$count]['price']=0;
					$array[$count]['date']=$v;
					$array[$count]['breakfast']=0;
				}
			}
			foreach($array AS $uniqid => $row){$arrSort[] = $row['date'];}
			array_multisort($arrSort, SORT_ASC, $array);
			$body = array('setRoomPriceItems' => array());
			$body['setRoomPriceItems'][0]['priceInfos'] = array();
			$body['setRoomPriceItems'][0]['hotelId'] = $hid;
			$body['setRoomPriceItems'][0]['sourceId'] = 2;
			$body['setRoomPriceItems'][0]['roomId'] = $rid;
			$body['setRoomPriceItems'][0]['startDate'] = $startDate.'T00:00:00';
			$body['setRoomPriceItems'][0]['endDate'] = $endDate.'T00:00:00';
			$body['setRoomPriceItems'][0]['currency'] = "CNY";
			$i=0;
			foreach($array as $k=>$v){
				$body['setRoomPriceItems'][0]['priceInfos'][$i]['prices'] = array();
				$body['setRoomPriceItems'][0]['priceInfos'][$i]['prices'][0]['amountBeforeTaxFee'] = $v['price'];
				$body['setRoomPriceItems'][0]['priceInfos'][$i]['prices'][0]['amountAfterTaxFee'] = $v['price'];
				$body['setRoomPriceItems'][0]['priceInfos'][$i]['prices'][0]['costAmountBeforeTaxFee'] = $v['price'];
				$body['setRoomPriceItems'][0]['priceInfos'][$i]['prices'][0]['costAmountAfterTaxFee'] = $v['price'];
				$body['setRoomPriceItems'][0]['priceInfos'][$i]['prices'][0]['day'] = 0;
				$body['setRoomPriceItems'][0]['priceInfos'][$i]['stays'] = 0;
				$body['setRoomPriceItems'][0]['priceInfos'][$i]['blanceType'] = "PP";
				$body['setRoomPriceItems'][0]['priceInfos'][$i]['priceType'] = "Cost";
				$body['setRoomPriceItems'][0]['priceInfos'][$i]['breakfast'] = $v['breakfast'];
				$body['setRoomPriceItems'][0]['priceInfos'][$i]['isCancel'] = 0;
				$i++;
			}
			$qmgTwoLog->typePrice($body);
		}
	}
	
	
	
	
	//关闭不可预定房态/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	//查询酒店提前预定天数
	function pytColseStatus(){
		$this->loadModel('qm','hotel');
		$in=$this->nowHotel();
		$sql="select hid,tqydts from qm_hotel where hid in(".$in.")";
		$sql_to="select count(id) as thcount from qm_hotel where hid in(".$in.")";
		$reto=$this->qm->hotelModel->fetchRow($sql_to);
		$count=$reto['thcount'];
		if($count<=50){
			$re=$this->qm->hotelModel->fetchAll($sql);
			$this->slrooms($re);
		}else{
			$page=ceil($count/50);
			for($i=0;$i<$page;$i++){
				$re=$this->qm->hotelModel->fetchAll($sql." limit ".($i*50).",50");
				$this->slrooms($re);
			}
		}
	}
	//查询酒店房型房态
	function slrooms($arr=array()){
		$this->loadModel('qm','roombatch');
		foreach($arr as $k=>$v){
			if($v['tqydts']>0){
				$sta=date('Y-m-d',time());$end="";
				$end=date('Y-m-d',strtotime("+".$v['tqydts']." day"));
				$sql="select * from qm_roombatch where hid='".$v['hid']."' and date>='".$sta."' and date<'".$end."' order by date asc";
				$re=$this->qm->roombatchModel->fetchAll($sql);
				$this->ST($re);
			}
		}
	}
	
	//推送满房房态
	function ST($resource=array()){
		$this->loadApi('toQmgHotelLog');
		$toQmgHotelLog = new toQmgHotelLog();
		
		$this->loadModel('qm','channels');
		$body['sourceId']=2;$body['editer']='qmg';$body['roomInfoList']=array();
		
		foreach($resource as $k=>$v){
			$body['hotelCode']=$v['hid'];
			$body['startDate']=$v['date'];
			$body['endDate']=$v['date'];
			$body['roomID']=$v['rid'];
			$body['roomInfoList'][0]['fl']=$v['maxNum'];
			$body['roomInfoList'][0]['setToDate']=$v['date'];
			$body['roomInfoList'][0]['changeDefault']='T';
			$body['roomInfoList'][0]['holdDeadline']='9999';
			$body['roomInfoList'][0]['note']='';
			$body['roomInfoList'][0]['reserveTime']='0';
			$body['roomInfoList'][0]['roomstatus']='N';
			$body['roomInfoList'][0]['roomsInv']='0';
			$body['roomInfoList'][0]['guarantee']='';
			$body['roomInfoList'][0]['guaranteeLCT']=4;
			$body['roomInfoList'][0]['prepayLCT']=23988;
			$body['roomInfoList'][0]['allNeedGuarantee']='F';
			$body['roomInfoList'][0]['lateReserveTime']=0;
			$body['roomInfoList'][0]['deductType']='C';
			$body['roomInfoList'][0]['blanceType']='PP';
			$body['roomInfoList'][0]['restorable']='F';
			$body['roomInfoList'][0]['checkType']='C';
			$body['roomInfoList'][0]['userLimited']='9999';
			$toQmgHotelLog->roomstatus($body);
		}
	}
}